package com.juli.v2xdemo.tcp.controller;

import android.content.Context;


import com.cz.basetool.CodeTool;
import com.example.tcpmaster.callback.AbsTcpActionCallBack;
import com.example.tcpmaster.callback.AbsTcpUICallBack;
import com.juli.v2xdemo.tcp.tcp_impl.TcpManager;

/**
 * Created by XinYi on 2019/1/14.
 * TCP 控制器
 */

public class TcpController {
    private static final TcpController ourInstance = new TcpController();

    public static TcpController getInstance() {
        return ourInstance;
    }

    private TcpController() {
    }


    /**
     * 连接服务器
     * @param ip
     * @param port
     * @param tcpActionCallBack
     */
    public void connect(Context context, String ip, int port, AbsTcpActionCallBack tcpActionCallBack, AbsTcpUICallBack tcpUICallBack){
        TcpManager.getInstance().setAutoReconnect(false);
        TcpManager.getInstance().connect(context,ip,port,tcpActionCallBack,tcpUICallBack);
    }

    /**
     * 连接服务器
     * @param tcpActionCallBack
     */
    public void connect(Context context, AbsTcpActionCallBack tcpActionCallBack, AbsTcpUICallBack tcpUICallBack,boolean b){
        TcpManager.getInstance().setAutoReconnect(b);
        TcpManager.getInstance().connect(context,tcpActionCallBack,tcpUICallBack);
    }

    public void disConnect(){
        TcpManager.getInstance().release();
    }

    /**
     * 发送HEX
     * @param str
     * @return
     */
    public boolean sendMsg(String str) {
        return TcpManager.getInstance().sendMsg(str);
    }

    /**
     * 发送NATIVE字符串
     * @param str
     * @return
     */
    public boolean sendMsg2(String str) {
        return TcpManager.getInstance().sendMsg(CodeTool.StringToHexString(str));
    }


    public void release() {
        TcpManager.getInstance().release();
    }
}
